package com.dst.smart.receiver;

import android.util.JsonReader;
import android.util.Log;

import java.io.File;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import com.dst.smart.R;
import com.dst.smart.app.MainActivity;
import com.dst.smart.app.MyApplication;
import com.dst.smart.dao.impl.IWidgetDAO;
import com.dst.smart.dao.impl.WidgetDAOProxy;
import com.dst.smart.dao.impl.factory.RoomFactory;
import com.dst.smart.dao.impl.factory.CusDeviceFactory;
import com.dst.smart.dao.impl.factory.DeviceFactory;
import com.dst.smart.dao.impl.factory.GateWayFactory;
import com.dst.smart.dao.impl.factory.ModeFactory;
import com.dst.smart.dao.impl.factory.ModeListFactory;
import com.dst.smart.dao.impl.factory.RedInfraFactory;
import com.dst.smart.dao.impl.factory.SubscribeFactory;
import com.dst.smart.dao.impl.factory.VoiceFactory;
import com.dst.smart.json.BasicInfoUtils;
import com.dst.smart.json.ProjectUtils;
import com.dst.smart.model.BasicInfo;
import com.dst.smart.model.GateWay;
import com.dst.smart.model.Room;
import com.dst.smart.music.MusicPlayMedia;
import com.dst.smart.json.AnHongUitils;
import com.dst.smart.json.MasterUtils;
import com.dst.smart.model.AnHong;
import com.dst.smart.model.Arce;
import com.dst.smart.model.CusDevice;
import com.dst.smart.model.Device;
import com.dst.smart.model.Master;
import com.dst.smart.model.Mode;
import com.dst.smart.model.ModeList;
import com.dst.smart.model.RedInfra;
import com.dst.smart.model.SubscribeInfo;
import com.dst.smart.model.VoiceInfo;
import com.dst.smart.service.TcpSender;
import com.dst.smart.socket.info.ISocketCode;
import com.dst.smart.tools.AppTools;
import com.dst.smart.tools.JSONHelper;

public class ResponseActionInfo {

	public static void addAnHong(String toID, String param) {

	}

	public static void getMasterInfo(String toID, String param) {
		try {
			String cmd = JSONHelper.toJSON(MasterUtils.getMaster(MyApplication
					.getInstance()));
			String result = ISocketCode.setMasterfo(cmd, toID);
			MainActivity.activity.sendCode(result);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void setMasterInfo(String toID, String param) {
		try {
			Master master = JSONHelper.parseObject(param, Master.class);
			if (master != null) {
				if (MasterUtils.masterInfo != null) {
					MasterUtils.masterInfo.setMn(master.getMn());
					MasterUtils.masterInfo.setPw(master.getPw());
					MasterUtils.masterInfo.setOr(master.getOr());
					MasterUtils.masterInfo.setOp(master.getOp());
					MasterUtils.saveMasterInfo();
					MainActivity.playSound(5, "用户权限修改成功");
					String result = ISocketCode.setLine(master.getMn(),
							master.getPw());
					MainActivity.activity.sendCode(result);
				} else {
					MainActivity.playSound(5, "用户权限修改失败");
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void addArce(String toID, String param) throws JSONException {
		//0803
			Room room = JSONHelper.parseObject(param, Room.class);
			if (room != null) {
				int num = Sqlite().insertRoom(room);
				if (num > 0) {
					String result = ISocketCode.setDownInfo("1", toID);
					MainActivity.activity.sendCode(result);
					RoomFactory.clearList();
				}
		}
	}
	public static void addDevice(String toID, String param) {
		try {
			//joe 0803
			Log.e("domtao",param);
			Device device = JSONHelper.parseObject(param, Device.class);
			if (device!=null) {
				int num0 = ModeFactory.judgeName(device.getDeviceName());
				if (num0 == 1) {
					MainActivity.playSound(5, device.getDeviceName() + "存在");
					String result = ISocketCode.setDownInfo("1", toID);
					MainActivity.activity.sendCode(result);
				} else {
					int num = Sqlite().insertDevice(device);
					if (num == 1) {
						if (MyApplication.getInstance().isDown == 0) {
							MainActivity
									.playSound(
											5,
											device.getDeviceName()
													+ MyApplication
															.getInstance()
															.getString(
																	R.string.addSuccess));
						}
						DeviceFactory.clearList();
						String result = ISocketCode.setDownInfo("1", toID);
						MainActivity.activity.sendCode(result);
					} else if (num == 2) {
						if (MyApplication.getInstance().isDown == 0) {
							MainActivity
									.playSound(
											5,
											device.getDeviceName()
													+ MyApplication
													.getInstance()
													.getString(
															R.string.updateSuccess));
						}
						DeviceFactory.clearList();
						String result = ISocketCode.setDownInfo("1", toID);
						MainActivity.activity.sendCode(result);
					} else {
						MainActivity
								.playSound(5, device.getDeviceName()
										+ MyApplication.getInstance()
												.getString(R.string.updateFail));
					}
				}
			} else {
				MainActivity.playSound(5, MyApplication.getInstance()
						.getString(R.string.updateFail));
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void updateDevice(String param) {
		try {
			Log.e("domtao",param);
			//joe 0803
				Device device =JSONHelper.parseObject(param,Device.class);
				if(device!=null) {
					String oldName = device.getOldName();
					if (DeviceFactory.updateName(device.getId(), device.getDeviceName(),
							oldName) != 0) {
						MainActivity.playSound(5, "设备列表已存在" + device.getDeviceName());
					} else if (ModeFactory.judgeName(device.getDeviceName()) != 0) {
						MainActivity.playSound(5, "情景模式已存在" + device.getDeviceName());
					} else {
						int num = Sqlite().updateDevice(device, oldName);
						if (num == 1) {
							MainActivity.playSound(5,
									oldName + "修改为" + device.getDeviceName());
							DeviceFactory.clearList();
						} else {
							MainActivity
									.playSound(5, device.getDeviceName()
											+ MyApplication.getInstance()
											.getString(R.string.updateFail));
						}
					}
				}else{
					MainActivity
							.playSound(5, device.getDeviceName()
									+ MyApplication.getInstance()
									.getString(R.string.updateFail));
				}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void updateSong(String param) {
		if (param.startsWith("song")) {
			try {
				if (MusicPlayMedia.getInstansMedia() == null) {
					new MusicPlayMedia(MainActivity.activity, "");
				}
				MusicPlayMedia.getInstansMedia().playURLList(null);
				MusicPlayMedia.getInstansMedia().setFileName(1,
						param.replace("song:", ""),0);
				MusicPlayMedia.getInstansMedia().play();

			} catch (Exception e) {
				// TODO: handle exception
			}
		} else if (param.startsWith("poetry")) {
			MainActivity.playSound(2, param.replace("poetry:", ""));
		} else if (param.startsWith("stop")) {
			MainActivity.UploadText(param.replace("stop:",""), 2,2);
		}else if(param.startsWith("vol")){
			/*
            int value=Integer.parseInt(param.replace("vol:", ""));
			if (MusicPlayMedia.getInstansMedia() != null) {
				MusicPlayMedia.setVolume(value);
			}
			MainActivity.playSound(4, "音量已设定:" +value );
			*/

			BasicInfo basicInfo= BasicInfoUtils.getBasic(MyApplication.getInstance());
			if(basicInfo!=null){
				basicInfo.setVol(param.replace("vol:",""));
				BasicInfoUtils.saveMasterInfo();
			}
			MainActivity.UploadText("音量"+param.replace("vol:",""), 2,2);
		}
	}

	public static void updateMode(String param) {
		try {
			//joe 0803
			Log.e("domtao",param);
			Mode mode=JSONHelper.parseObject(param,Mode.class);
		if(mode!=null){

				String oldName=mode.getOldName();
				if (ModeFactory.updateName(mode.getId(), mode.getModeName(), oldName) != 0) {
					MainActivity.playSound(5, "情景模式已存在" + mode.getModeName());
				} else if (DeviceFactory.judgeName1(mode.getModeName()) != 0) {
					MainActivity.playSound(5, "设备列表已存在" + mode.getModeName());
				} else {
					int num = Sqlite().updateMode(mode, oldName);
					if (num == 1) {
						MainActivity.playSound(5,
								oldName + "修改为" + mode.getModeName());
						ModeFactory.clearList();
					} else {
						MainActivity
								.playSound(5, mode.getModeName()
										+ MyApplication.getInstance()
												.getString(R.string.updateFail));
					}
				}
			} else {
				MainActivity.playSound(5, MyApplication.getInstance()
						.getString(R.string.updateFail));
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public static void updateAnHong(String param) {
		try {
			String[] results = param.split(":");
			if (results.length == 3) {
				AnHong anHong = new AnHong();
				anHong.setId(Integer.parseInt(results[0]));
				anHong.setMsgName(results[1]);
				anHong.setMsgInfo(results[2]);
				int num = AnHongUitils.setAnHong(anHong);
				if (num == 1) {
					MainActivity.playSound(5, "ID" + anHong.getMsgName()
							+ "修改成功!");
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void addMode(String toID, String param) {
		try {
			Log.e("domtao",param);
			//joe 0803
			Mode mode = JSONHelper.parseObject(param, Mode.class);
			if (mode != null) {
				int num = Sqlite().insertMode(mode);
				if (num == 1) {
					if (MyApplication.getInstance().isDown == 0) {
						MainActivity
								.playSound(5, mode.getModeName()
										+ MyApplication.getInstance()
												.getString(R.string.addSuccess));
					}
					ModeFactory.clearList();
					String result = ISocketCode.setDownInfo("1", toID);
					MainActivity.activity.sendCode(result);
				} else if (num == 2) {
					if (MyApplication.getInstance().isDown == 0) {
						MainActivity.playSound(
								1,
								mode.getModeName()
										+ MyApplication.getInstance().getString(
										R.string.updateSuccess));
					}
					ModeFactory.clearList();
					String result = ISocketCode.setDownInfo("1", toID);
					MainActivity.activity.sendCode(result);
				} else {
					MainActivity.playSound(
							5,
							mode.getModeName()
									+ MyApplication.getInstance().getString(
											R.string.updateFail));
				}
			} else {
				MainActivity.playSound(5, MyApplication.getInstance()
						.getString(R.string.updateFail));
				// String result = ISocketCode.setDownInfo("0", toID);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void addVoice(String param) {
		try {
			VoiceInfo voiceInfo = null;
			if(param.contains("{")||param.contains("}")){
				voiceInfo = (VoiceInfo) JSONHelper.parseObject(param, VoiceInfo.class);
			}
			else {
				voiceInfo = (VoiceInfo) JSONHelper.parseObject("{"
						+ param + "}", VoiceInfo.class);
			}
			if (voiceInfo != null) {
				int num = 0;
				if (voiceInfo.getId() > 0) {
					num = Sqlite().updateVoice(voiceInfo);
					if (num > 0) {
						MainActivity
								.playSound(
										5,
										"智能问答"
												+ MyApplication
														.getInstance()
														.getString(
																R.string.updateSuccess));
						VoiceFactory.clearList();
					} else {
						MainActivity
								.playSound(5, "智能问答"
										+ MyApplication.getInstance()
												.getString(R.string.updateFail));
					}

				} else {
					num = VoiceFactory.isExite(voiceInfo.getVoiceName(),
							voiceInfo.getVoiceAnswer());
					if (num == 0) {
						Sqlite().insertVoice(voiceInfo);
						MainActivity
								.playSound(5, "智能问答"
										+ MyApplication.getInstance()
												.getString(R.string.addSuccess));
						VoiceFactory.clearList();
					} else {
						MainActivity.playSound(
								5,
								"智能问答"
										+ MyApplication.getInstance()
												.getString(R.string.exist));
					}
				}
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void addSubScribe(String param) {
		try {
			String[] results = param.split(";");
			if (results.length == 7) {
				SubscribeInfo voice = new SubscribeInfo();
				voice.setId(Integer.parseInt(results[0]));
				voice.setSubscribeName(results[1]);
				voice.setSubscribeDate(results[2]);
				voice.setSubscribeTime(results[3]);
				voice.setSubscribeEvent(results[4]);
				voice.setSubscribeAction(results[5]);
				voice.setUserName(results[6]);

				int num = 0;
				if (voice.getId() > 0) {
					num = Sqlite().updateSubscribe(voice);
					if (num > 0) {
						MainActivity
								.playSound(
										5,
										"行程预约"
												+ MyApplication
														.getInstance()
														.getString(
																R.string.updateSuccess));
						SubscribeFactory.clearList();
					} else {
						MainActivity
								.playSound(5, "行程预约"
										+ MyApplication.getInstance()
												.getString(R.string.updateFail));
					}

				} else {
					num = SubscribeFactory.isExite(voice.getSubscribeName(),
							voice.getSubscribeEvent());
					if (num == 0) {
						Sqlite().insertSubscribe(voice);
						MainActivity
								.playSound(5, "行程预约"
										+ MyApplication.getInstance()
												.getString(R.string.addSuccess));
						SubscribeFactory.clearList();
					} else {
						MainActivity.playSound(
								5,
								"行程预约"
										+ MyApplication.getInstance()
												.getString(R.string.exist));
					}
				}
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void delVoice(String param) {
		try {

			int num = Sqlite().deleteVoice(Integer.parseInt(param));
			if (num > 0) {

				MainActivity.playSound(
						5,
						"智能问答"
								+ MyApplication.getInstance().getString(
										R.string.deleteSuccess));
				VoiceFactory.clearList();

			} else {
				MainActivity.playSound(
						5,
						"智能问答"
								+ MyApplication.getInstance().getString(
										R.string.deleteFail));
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void delSubscribe(String param) {
		try {

			int num = Sqlite().deleteSubscribe(Integer.parseInt(param));
			if (num > 0) {

				MainActivity.playSound(
						5,
						"行程预约"
								+ MyApplication.getInstance().getString(
										R.string.deleteSuccess));
				SubscribeFactory.clearList();

			} else {
				MainActivity.playSound(
						5,
						"行程预约"
								+ MyApplication.getInstance().getString(
										R.string.deleteFail));
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void getVoiceInfo(String toID) {
		try {
			List<VoiceInfo> list = VoiceFactory.getAll();
			if (list.size() > 0) {
				for (int i = 0; i < list.size(); i++) {
					VoiceInfo voice = list.get(i);
					String result = ISocketCode.setVoiceInfo(
							voice.convertTostring(), toID);
					MainActivity.activity.sendCode(result);
				}
			} else {
				MainActivity.playSound(5, "主机当前没有问答！");
				String result = ISocketCode.setVoiceInfo("主机当前没有问答", toID);
				MainActivity.activity.sendCode(result);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void getSubscribeInfo(String toID) {
		try {
			List<SubscribeInfo> list = SubscribeFactory.getAll();
			if (list.size() > 0) {
				for (int i = 0; i < list.size(); i++) {
					SubscribeInfo voice = list.get(i);
					String result = ISocketCode.setSubscribeInfo(
							voice.convertTostring(), toID);
					MainActivity.activity.sendCode(result);
				}
			} else {
				MainActivity.playSound(5, "主机当前没有行程预约！");
				String result = ISocketCode.setVoiceInfo("主机当前没有行程预约", toID);
				MainActivity.activity.sendCode(result);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void getApkVersion(String toID, String param) {
		try {
			MainActivity.activity.getApkVersion(toID, param);
		} catch (Exception e) {
		}
	}

	public static void updateApk(String toID, String param) {
		try {
			if (param.equals("AIUIService")) {
				MainActivity.playSound(5, "正在更新AIUIService服务");
				MainActivity.activity.downServiceShow(toID);
			} else {
				MainActivity.playSound(5, "正在更新软件");
				MainActivity.activity.downShow(toID);
			}
		} catch (Exception e) {
		}
	}

	public static void setCusDevice(String param) {
		try {
			JSONObject object = new JSONObject(param);
			String DeviceJson = object.getString("CusDeviceJson");
			List<CusDevice> list = (List<CusDevice>) JSONHelper
					.parseCollection(DeviceJson, List.class, CusDevice.class);

			Sqlite().updateCusDevice(list);
			MainActivity.playSound(5, "添加成功");
			CusDeviceFactory.clearList();

		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void setDeleteDevice(String param) {
		try {
			String[] results = param.split(";");
			if (results.length == 2) {
				int id = Integer.parseInt(results[0]);
				String deviceName = results[1];
				int result = MyApplication.getInstance().getWidgetDataBase()
						.deleteDevice(id);
				if (result > 0) {
					MainActivity.playSound(
							5,
							deviceName
									+ MyApplication.getInstance().getString(
											R.string.deleteSuccess));
					MyApplication.getInstance().getWidgetDataBase()
							.deleteRedFra(id);

					DeviceFactory.clearList();
					RedInfraFactory.clearList();
				}

			} else {
				MainActivity.playSound(5, MyApplication.getInstance()
						.getString(R.string.deleteFail));
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void setDeleteMode(String param) {
		try {
			String[] results = param.split(";");
			if (results.length == 2) {
				String deviceName = results[1];
				int result = MyApplication.getInstance().getWidgetDataBase()
						.deleteMode(Integer.parseInt(results[0]));

				if (result > 0) {
					MainActivity.playSound(
							5,
							deviceName
									+ MyApplication.getInstance().getString(
									R.string.deleteSuccess));
					ModeFactory.clearList();
				}

			} else {
				MainActivity.playSound(5, MyApplication.getInstance()
						.getString(R.string.deleteFail));
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}	public static void setDeleteModeList(String param) {
		try {
			String[] results = param.split(";");
			if (results.length == 2) {
				String deviceName = results[1];
				int result = MyApplication.getInstance().getWidgetDataBase()
						.deleteModelist(Integer.parseInt(results[0]));
				if (result > 0) {
					MainActivity.playSound(
							5,
							deviceName
									+ MyApplication.getInstance().getString(
									R.string.deleteSuccess));
					ModeListFactory.clearList();
				}

			} else {
				MainActivity.playSound(5, MyApplication.getInstance()
						.getString(R.string.deleteFail));
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void addModeList(String toID, String param) {
		try {
			//joe 0803
			ModeList modeList=JSONHelper.parseObject(param,ModeList.class);
			if (modeList!=null) {
				int num = Sqlite().insertModeList(modeList);
				if (num == 1) {
					String result = ISocketCode.setDownInfo("1", toID);
					MainActivity.activity.sendCode(result);
					ModeListFactory.clearList();
					if (MyApplication.getInstance().isDown == 0) {
						MainActivity.playSound(5, MyApplication.getInstance()
								.getString(R.string.addSuccess));
					}
				} else if (num == 2) {
					String result = ISocketCode.setDownInfo("1", toID);
					MainActivity.activity.sendCode(result);
					ModeListFactory.clearList();
					if (MyApplication.getInstance().isDown == 0) {
						MainActivity.playSound(5, MyApplication.getInstance()
								.getString(R.string.updateSuccess));
					}
				} else {
					// String result = ISocketCode.setDownInfo("0", toID);
					MainActivity.playSound(5, MyApplication.getInstance()
							.getString(R.string.updateFail));
				}
			} else {
				// String result = ISocketCode.setDownInfo("0", toID);
				MainActivity.playSound(5, MyApplication.getInstance()
						.getString(R.string.updateFail));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void addREdInfra(String toID, String param) {
		try {
			RedInfra infra=JSONHelper.parseObject(param,RedInfra.class);
			if(infra!=null) {
				int num = Sqlite().insertInfrad(infra);
				if (num > 0) {
					String result = ISocketCode.setDownInfo("1", toID);
					MainActivity.activity.sendCode(result);
					RedInfraFactory.clearList();
				} else {
					// String result = ISocketCode.setDownInfo("0", toID);
					MainActivity.playSound(5, MyApplication.getInstance()
							.getString(R.string.updateFail));
				}

			} else {
				// String result = ISocketCode.setDownInfo("0", toID);
				MainActivity.playSound(5, MyApplication.getInstance()
						.getString(R.string.updateFail));
			}
		} catch (Exception e) {

			e.printStackTrace();
		}
	}

	public static void setNameControl(String param) {
		if (param.startsWith("City")) {
			ProjectUtils.getRootPath().setCity(
					param.replace("City:",""));
			ProjectUtils.saveRootPath();
		} else if (param.startsWith("Ask")) {
			//SendCMD cmdsend = SendCMD.getInstance();
			//cmdsend.sendSubCmd(0, param.replace("Ask:",""), null,0);
			MainActivity.UploadText(param.replace("Ask:",""), 2,0);
		} else if (param.startsWith("Greeting")) {
			BasicInfo basicInfo=BasicInfoUtils.getBasic(MyApplication.getInstance());
			if(basicInfo!=null){
				basicInfo.setGreetingInfo(param.replace("Greeting:",""));
				BasicInfoUtils.saveMasterInfo();
				MainActivity.playSound(5, "问候语修改成功");
			}
		}else if (param.startsWith("WakeUp")) {
			BasicInfo basicInfo= BasicInfoUtils.getBasic(MyApplication.getInstance());
			if(basicInfo!=null){
				basicInfo.setWakeUpWord(param.replace("WakeUp:",""));
				BasicInfoUtils.saveMasterInfo();
				MainActivity.playSound(5, "唤醒应答修改成功");
			}
		}else{
			MainActivity.UploadText(param, 1,0);
		}
	}
	public static void getDevice(String toID, String param) {
		try {
			List<Device> list = DeviceFactory.getAll();
			if (list.size() > 1) {
				for (int i = 0; i < list.size(); i++) {
					Device device = list.get(i);
					String result = ISocketCode.setDeviceInfo(
							JSONHelper.toJSON(device), toID);
					MainActivity.activity.sendCode(result);
				}
			} else {
					MainActivity.playSound(5, "主机当前没有设备！");
					String result = ISocketCode.setDeviceInfo("当前没有设备", toID);
					MainActivity.activity.sendCode(result);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void getMode(String toID, String param) {
		try {
			//joe 0803
			List<Mode> list = ModeFactory.getAll();

			if (list.size() > 0) {
				for (int i = 0; i < list.size(); i++) {
					Mode mode = list.get(i);
					String result = ISocketCode.setModeInfo(
							JSONHelper.toJSON(mode), toID);
					MainActivity.activity.sendCode(result);
				}
			} else {
				MainActivity.playSound(5, "主机没有场景！");
				String result = ISocketCode.setDeviceInfo("主机没有场景", toID);
				MainActivity.activity.sendCode(result);

			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void getModeList(String toID, String param) {
		try {
			//joe 0803
				List<ModeList> list = ModeListFactory.getModeDevice(Integer
					.parseInt(param));
			if (list.size() > 0) {
				for (int i = 0; i < list.size(); i++) {
					ModeList modeList = list.get(i);
					String result = ISocketCode.setModeListInfo(JSONHelper.toJSON(modeList), toID);
					MainActivity.activity.sendCode(result);
				}
			} else {
				MainActivity.playSound(5, "当前模式没有设备！");
				String result = ISocketCode.setModeListInfo("当前模式没有设备！", toID);
				MainActivity.activity.sendCode(result);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void getAnHong(String toID, String param) {
		try {
			//joe 温湿度值
			if(param.equals("PcSensor")){
//joe 温湿度

				String cmd="insideTemp"+AppTools.getInts(MainActivity.activity.insideTemp);
				String result = ISocketCode.setDownApk(cmd, toID);
				MainActivity.activity.sendCode(result);

				cmd="Humidity"+AppTools.getInts(MainActivity.activity.Humidity);
				 result = ISocketCode.setDownApk(cmd, toID);
				MainActivity.activity.sendCode(result);

				cmd="pm25"+AppTools.getInts(MainActivity.activity.pm25);
				result = ISocketCode.setDownApk(cmd, toID);
				MainActivity.activity.sendCode(result);

				cmd="formaldehyde"+AppTools.getInts(MainActivity.activity.formaldehyde);
				result = ISocketCode.setDownApk(cmd, toID);
				MainActivity.activity.sendCode(result);

				cmd="illumination"+AppTools.getInts(MainActivity.activity.illumination);
				result = ISocketCode.setDownApk(cmd, toID);
				MainActivity.activity.sendCode(result);
			}else {
				List<AnHong> list = AnHongUitils.list;
				for (int i = 0; i < list.size(); i++) {
					String cmd = list.get(i).getId() + ":"
							+ list.get(i).getMsgName() + ":"
							+ list.get(i).getMsgInfo();
					String result = ISocketCode.setAnHongInfo(cmd, toID);
					MainActivity.activity.sendCode(result);
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void setDeleteAllDevice(String param) {
		try {
			// Sqlite().deleteSyncData("t_device");
			// Sqlite().deleteSyncData("t_mode");
			// Sqlite().deleteSyncData("t_mode_list");
			// Sqlite().deleteSyncData("t_infrared");
			String file = MyApplication.getInstance().getRootPath()
					+ File.separator + "homedatabases.db";
			File delFile = new File(file);
			if (delFile.exists()) {
				if (delFile.delete()) {
					// 刷新界面
					MainActivity.playSound(5, "设备清空成功！");
					MyApplication.getInstance().initFiles();
					WidgetDAOProxy.instance = null;
				}
			}
			DeviceFactory.clearList();
			ModeFactory.clearList();
			ModeListFactory.clearList();
			RedInfraFactory.clearList();
			VoiceFactory.clearList();

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void setDeleteAllVoice(String param) {
		try {
			Sqlite().deleteSyncData("t_voice");
			MainActivity.playSound(5, "智能问答清空成功！");
			VoiceFactory.clearList();

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void setSendCmd(String param) {
		try {
			JSONObject object = new JSONObject(param);
			String SendCmd = object.getString("SendCmd");
			TcpSender.sendMssageAF(2,SendCmd);

		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static String getDevices() {
		JSONObject jsonObject = new JSONObject();
		try {
			jsonObject.put("Command", "GetDevice");
			jsonObject.put("Param", "");
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return jsonObject.toString();
	}

	public static String setDownEnd(String msgStr) {
		JSONObject jsonObject = new JSONObject();
		try {
			jsonObject.put("Command", "SetDownEnd");
			jsonObject.put("Param", msgStr);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return jsonObject.toString();
	}

	public static String SetVoiceName(String voiceName) {

		try {
			JSONObject jsonObject = new JSONObject();
			jsonObject.put("Command", "SetVoiceName");
			jsonObject.put("Param", voiceName);
			return jsonObject.toString();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "";
	}

	public static void setDeviceTimimg(String param) {
		try {
			JSONObject object = new JSONObject(param);
			String netCode = object.getString("netCode");
			// SendCMD cmdsend = SendCMD.getInstance();
			// cmdsend.SendToCMD(MainActivity.activity, netCode, false, 0, -1);

			MainActivity.playSound(5, netCode);
		} catch (Exception e) {
			// TODO: handle exception
		}
	}

	public static String setReturnCode(String code) {
		JSONObject jsonObject = new JSONObject();
		try {
			jsonObject.put("Command", "ReturnCode");
			JSONObject paramObject = new JSONObject();
			paramObject.put("Code", code);
			jsonObject.put("Param", paramObject.toString());
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return jsonObject.toString();
	}

	public static String cusDevices() {
		JSONObject jsonObject = new JSONObject();
		try {
			jsonObject.put("Command", "CusDeviceInfo");
			jsonObject.put("Param",
					JSONHelper.toJSON(CusDeviceFactory.getAll()));

		} catch (Exception e) {
		}

		return jsonObject.toString();
	}
	public static void getGateWay(String toID){
		List<GateWay> list= GateWayFactory.getAll();
		if (list.size() > 0) {
			for (int i = 0; i < list.size(); i++) {
				GateWay device = list.get(i);
				String result = ISocketCode.setGateWayInfo("device:"+
						device.convertTostring(), toID);
				MainActivity.activity.sendCode(result);
			}
		} else {
			MainActivity.playSound(5, "主机当前没有网关设备！");
			String result = ISocketCode.setGateWayInfo("Err:当前没有网关设备", toID);
			MainActivity.activity.sendCode(result);
		}
	}
	public static void addGateWay(String content){
		String[] str=content.trim().split(";");
		if(str.length==9) {
			GateWay gateWay=new GateWay();
			gateWay.setGatewayName(str[1]);
			gateWay.setGatewayID(str[2]);
			gateWay.setUserName(str[3]);
			gateWay.setUserPassWord(str[4]);
			gateWay.setTypeId(Integer.parseInt(str[5]));
			gateWay.setCreationTime("00:00:00");
			gateWay.setIsCurrent(Integer.parseInt(str[6]));
			gateWay.setState(str[7]);
			gateWay.setSeqencing(Integer.parseInt(str[8]));
			int num=Sqlite().insertGateWay(gateWay);
			if(num==-1) {
				MainActivity.playSound(5, "网关存在!");
			}else{
				MainActivity.playSound(5, "网关添加成功!");
				GateWayFactory.clearList();
			}
		}
	}
	public static void updateGateWay(String content){
		String[] str=content.trim().split(";");
		if(str.length==9) {
			GateWay gateWay=new GateWay();
			gateWay.setId(Integer.parseInt(str[0]));
			gateWay.setGatewayName(str[1]);
			gateWay.setGatewayID(str[2]);
			gateWay.setUserName(str[3]);
			gateWay.setUserPassWord(str[4]);
			gateWay.setTypeId(Integer.parseInt(str[5]));
			gateWay.setCreationTime("00:00:00");
			gateWay.setIsCurrent(Integer.parseInt(str[6]));
			gateWay.setState(str[7]);
			gateWay.setSeqencing(Integer.parseInt(str[8]));
			int num=Sqlite().updateGateWay(gateWay,"");
			if(num>0) {
				MainActivity.playSound(5, "网关修改成功!");
				GateWayFactory.clearList();
			}else{
				MainActivity.playSound(5, "网关修改失败!");
			}
		}
	}
	public static void deleteGateWay(String content){
		int num=Sqlite().deleteGateWay(Integer.parseInt(content.trim()));
		if(num>0) {
			MainActivity.playSound(5, "网关删除成功!");
			GateWayFactory.clearList();
		}else{
			MainActivity.playSound(5, "网关删除失败!");
		}
	}

	public static IWidgetDAO Sqlite() {
		return MyApplication.getInstance().getWidgetDataBase();
	}

}
